package com.youhome.service.service.impl;

import com.youhome.common.exception.CustomException;
import com.youhome.common.response.ResultCode;
import com.youhome.service.mapper.HouseFavouriteMapper;
import com.youhome.service.model.BUser;
import com.youhome.service.model.HouseFavourite;
import com.youhome.service.model.ListHub;
import com.youhome.service.model.Principal;
import com.youhome.service.service.HouseFavouriteService;
import com.youhome.service.service.ListHubService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by chen365 on 2018/08/23.
 */
@Service
public class HouseFavouriteServiceImpl implements HouseFavouriteService {
    @Autowired
    private HouseFavouriteMapper houseFavouriteMapper;
    @Autowired
    private ListHubService listHubService;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return houseFavouriteMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(HouseFavourite record) {
        record.setCreateTime(new Date());
        return houseFavouriteMapper.insert(record);
    }

    @Override
    public HouseFavourite selectByPrimaryKey(Long id) {
        return houseFavouriteMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<String> selectHouseIdByUserId(Long userId) {
        return houseFavouriteMapper.selectHouseIdByUserId(userId);
    }


    @Override
    public void add(Principal principal,String id) throws CustomException {
        Long userId = principal.getUser().getUserId();

        ListHub listHub = listHubService.selectByPrimaryKey(id);
        if (listHub == null) {
            throw new CustomException(ResultCode.HOUSE_INEXISTENCE);
        }

        HouseFavourite record = new HouseFavourite();
        record.setHouseId(id);
        record.setUserId(userId);

        HouseFavourite houseFavourite = houseFavouriteMapper.selectByHouseIdAndUserId(record);
        if (houseFavourite != null) {
            throw new CustomException(ResultCode.HAVE_FAVOURITE);
        }
        if (houseFavouriteMapper.insert(record) == 0) {
            throw new CustomException(ResultCode.FAIL);
        }
    }

    @Override
    public void delete(Principal principal,String id) throws CustomException {

        Long userId = principal.getUser().getUserId();

        HouseFavourite record = new HouseFavourite();
        record.setUserId(userId);
        record.setHouseId(id);

        HouseFavourite houseFavourite = houseFavouriteMapper.selectByHouseIdAndUserId(record);
        if (houseFavourite == null) {
            throw new CustomException(ResultCode.NOT_FAVOURITE_HOUSE);
        }
        if (houseFavouriteMapper.deleteByPrimaryKey(houseFavourite.getId()) == 0) {
            throw new CustomException(ResultCode.FAIL);
        }
    }

    @Override
    public List<String> selectAllFavouriteHouseId(Principal principal) throws CustomException {
        Long userId = principal.getUser().getUserId();
        return houseFavouriteMapper.selectHouseIdByUserId(userId);
    }

    @Override
    public void saveFavorite(Principal principal, String action, String houseId) throws CustomException {

        ListHub house = listHubService.selectByPrimaryKey(houseId);
        if (house == null) {
            throw new CustomException(ResultCode.HOUSE_INEXISTENCE);
        }
        Long userId = principal.getUser().getUserId();

        HouseFavourite record = new HouseFavourite();
        record.setHouseId(houseId);
        record.setUserId(userId);
        HouseFavourite favourite = houseFavouriteMapper.selectByHouseIdAndUserId(record);

        if (action.equalsIgnoreCase("save")) {
            if (favourite != null) {
                throw new CustomException(ResultCode.HAVE_FAVOURITE);
            } else {
                if (houseFavouriteMapper.insert(record) == 0) {
                    throw new CustomException(ResultCode.FAIL);
                }
            }
        } else if (action.equalsIgnoreCase("unsave")) {
            if (favourite != null) {
                if (houseFavouriteMapper.deleteByPrimaryKey(favourite.getId()) == 0) {
                    throw new CustomException(ResultCode.FAIL);
                }
            } else {
                throw new CustomException(ResultCode.NOT_FAVOURITE);
            }
        } else {
            throw new CustomException(ResultCode.FAIL);
        }
    }

    @Override
    public void saveFavorite(BUser user, String action, String houseId) throws CustomException {
        ListHub house = listHubService.selectByPrimaryKey(houseId);
        if (house == null) {
            throw new CustomException(ResultCode.HOUSE_INEXISTENCE);
        }

        HouseFavourite record = new HouseFavourite();
        record.setHouseId(houseId);
        record.setUserId(user.getUserId());
        HouseFavourite favourite = houseFavouriteMapper.selectByHouseIdAndUserId(record);

        if (action.equalsIgnoreCase("save")) {
            if (favourite != null) {
                throw new CustomException(ResultCode.HAVE_FAVOURITE);
            } else {
                if (houseFavouriteMapper.insert(record) == 0) {
                    throw new CustomException(ResultCode.FAIL);
                }
            }
        } else if (action.equalsIgnoreCase("unsave")) {
            if (favourite != null) {
                if (houseFavouriteMapper.deleteByPrimaryKey(favourite.getId()) == 0) {
                    throw new CustomException(ResultCode.FAIL);
                }
            } else {
                throw new CustomException(ResultCode.NOT_FAVOURITE);
            }
        } else {
            throw new CustomException(ResultCode.FAIL);
        }
    }
}
